package com.algorithm;

import java.util.HashSet;
import java.util.Set;

/**
 * @author: sun.hongliang
 * @create: 2019/11/11 11:04
 */
public class IntersectionNodeTest {

    public static void main(String[] args) {

    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> listNodeSet = new HashSet<>();
        ListNode p = headA;
        while (p != null) {
            listNodeSet.add(p);
            p = p.next;
        }
        ListNode q = headB;
        while (q != null) {
            if (listNodeSet.contains(q)) {
               return q;
            }
            q = q.next;
        }
        return null;
    }
}
